#include<bits/stdc++.h>
using namespace std;
int rd(){
	int ans=0,f=1;char c=getchar();
	while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
	while(c>47&&c<58)ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
	return ans*f;
}
#define f(a,b,c) for(int a=b;a<=c;++a)
#define F(a,b,c) for(int a=b;a>=c;--a)
#define int long long
bool aa;
int dp[2055][2][2055];//,mx11[105][105],mx12[105][105];
int dp2[200005][2][11];//,mx21[105][105],mx22[105][105];
int a[200005];
bool bb;
signed main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	//cout<<(&bb-&aa)/1024.0/1024.0<<endl;
	int T=rd();
	f(I,1,T){
		int n=rd();
		f(i,1,n)a[i]=rd();
		if(n<=2024){
			memset(dp,-0x3f,sizeof dp);
			// as red and blue are similar, we force a[1] to be painted red(0)
			dp[1][1][0]=0;
			f(i,2,n)dp[i][1][a[i-1]]=dp[i-1][1][a[i-2]]+a[i-1]*(a[i-1]==a[i-2]);// 1 to i-1 is red, i is blue
			int ans=dp[n][1][a[n-1]]+a[n]*(a[n]==a[n-1]);// all colored red
			f(i,3,n)f(j,0,1){
				f(k,1,2000){
					// last==j
					dp[i][j][k]=dp[i-1][j][k]+(a[i]==a[i-1])*a[i];
				}
				// last!=j
				f(k,1,2000)dp[i][j][a[i-1]]=max(dp[i][j][a[i-1]],dp[i-1][j^1][k]+(k==a[i])*k);
			}
			f(i,0,1)f(j,1,2000)ans=max(ans,dp[n][i][j]);
			printf("%lld\n",ans);
		}
		else if(a[1]<=10){
			memset(dp2,-0x3f,sizeof dp2);
			// as red and blue are similar, we force a[1] to be painted red(0)
			dp2[1][1][0]=0;
			f(i,2,n)dp2[i][1][a[i-1]]=dp2[i-1][1][a[i-2]]+a[i-1]*(a[i-1]==a[i-2]);// 1 to i-1 is red, i is blue
			int ans=dp2[n][1][a[n-1]]+a[n]*(a[n]==a[n-1]);// all colored red
			f(i,3,n)f(j,0,1){
				f(k,1,10){
					// last==j
					dp2[i][j][k]=dp2[i-1][j][k]+(a[i]==a[i-1])*a[i];
				}
				// last!=j
				f(k,1,10)dp2[i][j][a[i-1]]=max(dp2[i][j][a[i-1]],dp2[i-1][j^1][k]+(k==a[i])*k);
			}
			f(i,0,1)f(j,1,10)ans=max(ans,dp2[n][i][j]);
			printf("%lld\n",ans);
		}
	}
	return 0;
}
/*
sb3KBsf8AhSYWjDHYzW957bNLfE9ToQJOTdXLIgTz82TpFkMFGTIM4dhhpqAeES0f5XzMBSJKUn7iVRvkuD6wolddbmFnEFSHnRbysvGMgO2BpxtiAzeykGaJ2p4E5VTsw3qmy59cRBlE7fwFcZCLnmcpbh1gCSW6VLtRQ03r9p5GS0W4ZiNMU093f8iraEv4ZmVxkXosKs8mscoPICjkCsoryWgWiZaGK4D4b0wvs3fidT7ufqepgQecuI8dshtCl6EK69FwAsenLjhzZKoDkRpdjwGbd7lMBygG7tCHjp24Yi362qHkH7N01baciLwuJaAOTKfA9gEgOGmO638L8UJ95tjoFfgWFovg8YqGC3waJgwNj3iqX1xcthO6K4casVEz25FE89pPpjmWmSMJRIuKzIQJN1rDW6AYBPnHWagLJQq6sANJSHeRa2jv3a6aeH6per4BRiUj8InyTAIvQuL0w2t0cxYFEc4rU70tnScwlPuCYA7y2QwzSnx3kTIz5KoXPoRDG171PzDx8JtAhp98c49MXPJbh787tZIAbN90MLvUeodJbmRDpaXMxpxfu6kod2wEP5CjQ7C2tpKUZlfOKDjF0euukegMecY1gll4sv5kkPEH8ugQ5Pg53axmDe6pEeruPaxGV20DREUX68yBXeEZEAJPmPF034tReqh7qfIFtmmyvk7QOMxbUE0GdDEfH56tvn0jSIyJ3lFwVNKshGTBURP6ddlujroccmM23IJ43X0XuIPZYsk3H79ukunRjb4JLOJMgaQk7OHzheYFUIIlPPg8HTZy2dFLzYig76OEVdCCHkPl1iUQ5AWNb6LbhZxf7dLCjhPCKbOZLoKMUolXxHuWLE81n3gsgz5NgUa03wZOkrlCfUkalc75qD6bGkUujXHP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